import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@WebServlet("/del_prompt")

public class DelPrompt extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");
        if (id == null) {
            throw new ServletException("please input id");
        }

        // 1.在redis中删除key为此id的数据
        /*
            请完善代码
        */
		delKey("id");




        
        // 2.删除数据库中此id的数据
        delPrompt(getJDBCConnection(), id);

        PrintWriter out = resp.getWriter();
        out.printf("[del]ok");
        out.close();

    }

    public void delPrompt(final Connection conn, String id) throws ServletException {
        /*
            请完善代码
            删除
        */

 	   Statement stmt = null;
        int row = 0;
        try {
            stmt = conn.createStatement();
            String sql = String.format("delete from t_ai_painting where id = %d",Integer.valueOf(id));
            row = stmt.executeUpdate(sql);

          if (row == 0) {
                throw new ServletException("delete error.");
            }
        } catch (Exception e) {
            throw new ServletException(e);
        } finally {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



       
        }

    }


